﻿(function () {
	$(function () {

        var _TestInfoService = abp.services.app.testInfo;
        var _$modal = $('#RoleCreateModal');
		var _$form = _$modal.find('form');

		_$form.validate({
		});

		 
        $('.delete-role').click(function () {
            var roleId = $(this).attr("data-role-id"); 

			deleteRole(roleId);
		});

		$('.edit-role').click(function (e) {
			var roleId = $(this).attr("data-role-id");

			e.preventDefault();
			$.ajax({
                url: abp.appPath + 'TestInfo/EditTestInfoModal?Id=' + roleId,
				type: 'POST',
				contentType: 'application/html',
				success: function (content) {
					$('#RoleEditModal div.modal-content').html(content);
				},
				error: function (e) { }
			});
		});

		_$form.find('button[type="submit"]').click(function (e) {
			e.preventDefault();

			 

			var role = _$form.serializeFormToObject(); //serializeFormToObject is defined in main.js
             
			abp.ui.setBusy(_$modal);
            _TestInfoService.create(role).done(function () {
				_$modal.modal('hide');
				location.reload(true); //reload page to see new role!
			}).always(function () {
				abp.ui.clearBusy(_$modal);
			});
		});

		_$modal.on('shown.bs.modal', function () {
			_$modal.find('input:not([type=hidden]):first').focus();
		});

		function refreshRoleList() {
			location.reload(true); //reload page to see new role!
		}

        function deleteRole(id) {
			abp.message.confirm(
                abp.utils.formatString(abp.localization.localize('AreYouSureWantToDelete', 'CbtMpwaSys'), "此信息"),
				function (isConfirmed) {
					if (isConfirmed) {
                        _TestInfoService.delete({
							id: id
						}).done(function () {
							refreshRoleList();
						});
					}
				}
			);
		}
	});
})();